package com.jwdf.uda.ui.fragment.progress;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.databinding.FragmentLongTaskBinding;
import com.jwdf.uda.event.LongTaskEvent;
import com.jwdf.uda.ui.adapter.LongTaskListAdapter;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.EventBusUtil;

import java.util.List;

/**
 * Created by Administrator on 2016/10/27.
 */
public class LongTaskFragment extends BaseFragment {

    FragmentLongTaskBinding mBinding;
    LongTaskListAdapter mAdapter;
    TaskList mTaskList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_long_task, container, false);
        EventBusUtil.registerEvent(this);
        initView();
        return mBinding.getRoot();
    }
    public void initView() {
        if(LongTask.getInstance().getListList().size() != 0) {
            initTaskList(LongTask.getInstance().getListList());
        }
    }
    /**
     * 加载投票信息
     */
    private void initTaskList(List<TaskList> taskLists) {
        mAdapter=new LongTaskListAdapter(getActivity());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mBinding.pullLoadMoreRecyclerView.setLayoutManager(linearLayoutManager);
        mBinding.pullLoadMoreRecyclerView.setAdapter(mAdapter);
        mAdapter.setDetailData(taskLists);

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBusUtil.unregisterEvent(this);
    }
    public void onEventMainThread(LongTaskEvent event) {
        switch(event.getEvent()) {
            case LongTaskEvent.LONG_TASK_QUESTION_CLOSE:
                LongTask.getInstance().deleteTaskList(event.getmName());
                if(LongTask.getInstance().getListList().size() == 0) {
                    EventBusUtil.post(new LongTaskEvent(LongTaskEvent.LONG_TASK_QUESTION_COMPLETE));
                    getActivity().finish();
                } else {
                    loadingTaskList(LongTask.getInstance().getListList());
                }
                break;
        }
    }
    private void loadingTaskList(List<TaskList> taskLists) {
        mAdapter.setDetailData(taskLists);
    }
}
